package com.jdyinfo.ic.controller;

import java.sql.Timestamp;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.jdyinfo.ic.mysql.entity.UpTown;
import com.jdyinfo.ic.service.UpTownService;
import com.jdyinfo.ic.util.IcUtil;

import io.swagger.annotations.Api;

@Api(value="/uptown",tags="dfds")
@RestController
@EnableAutoConfiguration
@RequestMapping("/")
public class UpTownController {
	Logger logger = LoggerFactory.getLogger(UpTownController.class);
	
	@Autowired
	ObjectMapper objectMapper;
	
	@Autowired
	UpTownService upTownService;
	
	@PostMapping(value = "/uptown", consumes = "application/json; charset=utf-8")
	int createUpTown(@RequestBody UpTown upTown) {
		int tenantId = 1;
		Timestamp nowTimestamp = IcUtil.nowTimestamp();
		String createdBy = "-1";
		String lastUpdatedBy = "-1";
		
		upTown.setTenantId(tenantId);
		upTown.setCreatedBy(createdBy);
		upTown.setCreatedDate(nowTimestamp);
		upTown.setLastUpdatedBy(lastUpdatedBy);
		upTown.setLastUpdatedDate(nowTimestamp);
		
		return upTownService.createUpTown(upTown);
	}
	
	@GetMapping(value = "/uptown")
	List<UpTown> getUpTown() {
		int tenantId = 1;
		
		return upTownService.findUpTown(tenantId);
	}
	
	@PostMapping("/uptown/{id}")
	int changeUpTown(@PathVariable("id") int id, @RequestBody UpTown upTown) {
		String lastUpdatedBy = "-1";
		Timestamp lastUpdatedDate = IcUtil.nowTimestamp();
		
		upTown.setId(id);
		upTown.setLastUpdatedBy(lastUpdatedBy);
		upTown.setLastUpdatedDate(lastUpdatedDate);
		
		return upTownService.updateUpTown(upTown);
	}
	
}
